﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QQ.InterviewQuestions.RandomShuffle
{
    internal class RandomShuffle
    {
        private int mNumberCards;

        public RandomShuffle(int n)
        {
            mNumberCards = n;
        }

        public int[] Execute()
        {
            int[] cards = new int[mNumberCards];
            for (int i = 0; i < cards.Length; i++)
            {
                cards[i] = i;
            }

            Random o = new Random();
            for (int i = 0; i < cards.Length - 1; i++)
            {
                int random;
                random = o.Next(cards.Length - i);
                Swap(ref cards[cards.Length - 1 - i], ref cards[random]);
            }

            return cards;
        }

        private void Swap(ref int a, ref int b)
        {
            int temp;
            temp = a;
            a = b;
            b = temp;
        }
    }
}
